package com.cyh.core.framework.common.utils;

import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * CSV工具类
 * @Author: yanhang_chen
 * @Date: 2020-09-16 16:31:39
 */
public class CSVUtil {

    private static final char SEPARATOR = ',';
    public static final String UTF8 = "UTF-8";
    public static final String GBK = "GBK";
    public static final String ASCII = "ASCII";
    public static final String GB2312 = "GB2312";

    /**
     * 读取csv文件标题
     * @param csvFilePath
     * @param code csv文件的编码,如utf8,,gbk
     * @return 返回csv文件的标题
     * @throws Exception
     */
    public static List<String> getHeader(String csvFilePath, String code) throws Exception{
        InputStream inputStream = new FileInputStream(new File(csvFilePath));
        List<List<String>> csvList = new ArrayList<>();
        CsvReader reader = new CsvReader(inputStream, SEPARATOR, Charset.forName(code));
        while(reader.readRecord()){
            csvList.add(new ArrayList<>(Arrays.asList(reader.getValues())));
        }
        reader.close();
        return csvList.get(0);
    }

    /**
     * 读取csv文件内容
     * @param csvFilePath
     * @param code csv文件的编码,如utf8,,gbk
     * @return 返回csv文件中的数据
     * @throws Exception
     */
    public static List<List<String>> readAsList(String csvFilePath, String code) throws Exception{
        InputStream inputStream = new FileInputStream(new File(csvFilePath));

        //1. 存储csv文件中的内容
        List<List<String>> csvList = new ArrayList<>();

        //2. 创建CsvReader
        CsvReader reader = new CsvReader(inputStream, SEPARATOR, Charset.forName(code));

        //3. 跳过表头,如果需要表头的话，不要写这句
        reader.readHeaders();

        //4.逐行读入除表头的数据
        while(reader.readRecord()){
            csvList.add(new ArrayList<>(Arrays.asList(reader.getValues())));
        }

        //5. 释放资源
        reader.close();
        return csvList;
    }

    /**
     * 读取csv文件内容
     * @param csvFilePath
     * @param code csv文件的编码,如utf8,,gbk
     * @return 返回csv文件中的数据
     * @throws Exception
     */
    public static List<HashMap<String,String>> readAsMap(String csvFilePath, String code) throws Exception{
        InputStream inputStream = new FileInputStream(new File(csvFilePath));

        //1. 存储csv文件中的内容
        List<List<String>> csvList = new ArrayList<>();
        List<HashMap<String,String>> hashMapList = new ArrayList<>();

        //2. 创建CsvReader
        CsvReader reader = new CsvReader(inputStream, SEPARATOR, Charset.forName(code));

        while(reader.readRecord()){
            csvList.add(new ArrayList<>(Arrays.asList(reader.getValues())));
        }

        List<String> headers = csvList.get(0);

        csvList.remove(0);

        for (List<String> l:csvList) {
            int i=0;
            HashMap<String,String> map = new HashMap<>();
            for (String header: headers) {
                map.put(header,l.get(i));
                i++;
            }
            hashMapList.add(map);
        }

        //5. 释放资源
        reader.close();
        return hashMapList;
    }

    /**
     * 数据写入csv文件
     * @param list UTF-8编码写入csv文件的内容
     * @param filePath 写入的csv文件的指定路劲
     * @throws Exception
     */
    public static void write(List<List<String>> list, String filePath,String code) throws Exception{
        CsvWriter wr = new CsvWriter(filePath, ',', Charset.forName(code));
        for (List<String> slist:list) {
            wr.writeRecord(slist.toArray(new String[0]));
        }
        wr.close();
    }
}
